package com.atguigu01.use;

import org.junit.Test;

import java.util.*;

/**
 * @author 尚硅谷-宋红康
 * @create 11:39
 */
public class CollectionMapTest {

    //体会集合中使用泛型前的场景

    @Test
    public void test1(){
        List list = new ArrayList();
        list.add(67);
        list.add(78);
        list.add(76);
        list.add(99);

        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            Integer next = (Integer) iterator.next();
            int score = next;
            System.out.println(next);
        }

    }


    //在集合中使用泛型的例子

    public void test2(){
        List<Integer> list = new ArrayList<Integer>();

        list.add(78);
        list.add(76);
        list.add(66);
        list.add(99);
        //编译报错，保证类型的安全
//        list.add("AA");

        Iterator<Integer> iterator = list.iterator();
        while(iterator.hasNext()){
            //因为添加的都是Integer类型，避免了强转操作
            Integer i = iterator.next();
            int score = i;

            System.out.println(score);
        }

    }

    @Test
    public void test3() {

        //jdk7的新特性
        HashMap<String,Integer> map = new HashMap<>(); //类型推断

        map.put("Tom",67);
        map.put("Jerry",87);
        map.put("Rose",99);

        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        Iterator<Map.Entry<String, Integer>> iterator = entries.iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Integer> next = iterator.next();
            System.out.println(next.getKey() + "----" + next.getValue());
        }

    }


}
